<?php
/**
 * Класс обработки AJAX запросов
 * @author mrpie
 */
class AJAX extends BasicAJAX {
	
	public function __construct(Core $Site) {
		parent::__construct($Site);
		$this->AjaxQuerys = array(
			'admin_login' => 'AdminLogin'
		);
	}
	public function CallFunc() {
		if (isset($this->AjaxQuerys[$this->URL->Params['ajax']])) {
			if (is_callable(array($this, $this->AjaxQuerys[$this->URL->Params['ajax']]))) {
				call_user_func_array(array($this, $this->AjaxQuerys[$this->URL->Params['ajax']]), array());
			} else {$this->RequestArr['ajaxMsg'] = 'Не найден обработчик AJAX запроса';}
		} else {$this->RequestArr['ajaxMsg'] = 'Не найден обработчик AJAX запроса';}
	}
	private function AdminLogin() {
		$Login	= md5(trim($this->URL->Params['login']));
		$Pass	= md5($Login.trim($this->URL->Params['pass']));
		$Select = $this->MySQL->Select('SELECT * FROM `{p}users` WHERE `USER_LOGIN` = "'.$Login.'" AND `USER_PASS` = "'.$Pass.'"');
		if ($Select) {
			$this->RequestArr['ajaxRez'] = 'true';
			$_SESSION['USER_LOGIN'] = array(
				'XSID'						=> session_id(),
				'XUID'					=> md5($Select['ID']),
				'XSUL'					=> $Login
			);
			$time = time() + $this->AdminConfig->Site['COOKIE_LIFE'];
			setcookie('XSID', session_id(), $time);
			setcookie('XUID', $_SESSION['USER_LOGIN']['XUID'], $time);
			setcookie('XSUL', $Login, $time);
			// установка COOCIE
			$this->MySQL->Query('INSERT INTO `{p}user_sessions` (`ID`, `XUID`, `XSID`, `BROWSER`, `IP`, `LAST_VISIT`) VALUES (NULL, "'.$_SESSION['USER_LOGIN']['XUID'].'", "'.$_SESSION['USER_LOGIN']['XSID'].'", "'.$_SERVER['HTTP_USER_AGENT'].'", "'.$_SERVER['REMOTE_ADDR'].'", "'.date('Y-m-d H:i:s').'")');
			$_SESSION['USER_LOGIN']['ID'] = $this->MySQL->LastID();
			echo $this->MySQL->LastSQL();
		} else {
			unset($_SESSION['LOGIN_USER']);
			$this->RequestArr['ajaxRez'] = 'false';
			$this->RequestArr['ajaxMsg'] = 'Неверный логин или пароль.';
		}
	}
}
?>